<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>混入机制——代码复写</title>
</head>
<body>
<div id="app">
    <cpn1></cpn1>
    <cpn2></cpn2>
</div>

<template id="cpn1">
    <h1 v-on:click="headClicked">这是组件1</h1>
</template>

<template id="cpn2">
    <h1 v-on:click="headClicked">这是组件2</h1>
</template>

<script type="module">
    import {createApp, ref} from "../../../../js/vue.esm-browser.js";
    import {defineComponent} from "../../../../js/vue.esm-browser.js";

    let mixin = {
        data() {
            let message = ref("Hello, World!")
            return {
                message,
            }
        },
        methods: {
            headClicked() {
                console.log("点击了标题");
            }
        }
    }


    let cpn1 = defineComponent(
        {
            template: "#cpn1",
            setup() {
                return {};
            },
            mixins: [mixin],
            // 当混入的内容和组件自定义内容相冲突的时候，以自定义的内容为主。
            methods: {
                headClicked() {
                    console.log("点击了组件 1 中标题");
                }
            }
        }
    )

    let cpn2 = defineComponent(
        {
            template: "#cpn2",
            setup() {
                return {};
            },
            mixins: [mixin],
            methods: {
                headClicked() {
                    console.log("点击了组件 2 中标题");
                }
            }
        }
    )


    let app = createApp(
        {
            setup() {
                return {}
            },
            components: {
                cpn1,
                cpn2,
            }
        }
    )

    app.mount("#app")
</script>
</body>
</html>